const crypto = require('crypto')
const jwt = require('jsonwebtoken')

const {PRIVATE_KEY} = require('../utils/constant')

// 判断是否为对象,精确的判断数据类型
function isObject(o) {
  return Object.prototype.toString.call(o) === '[object Object]'
}


// 参数为 string类型
function md5(s) {
  return crypto.createHash('md5').update(String(s)).digest('hex')
}

function decoded(req) {
  let token = req.get('Authorization')
  if (token.indexOf('Bearer') === 0) {
    token = token.replace('Bearer ', '')
  }
  return jwt.verify(token, PRIVATE_KEY)
}

module.exports = {
  md5,
  decoded,
  isObject
}